package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jf3q.common.domain.EduSubject;
import com.jf3q.common.mapper.EduSubjectMapper;
import com.jf3q.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    EduSubjectMapper subjectMapper;

    @GetMapping("/getAllSubject")
    public ResultVo getAllSubject() {
        // 查询所有学科
        List<EduSubject> allList = subjectMapper.selectList(new QueryWrapper<EduSubject>());

        List<EduSubject> list=new ArrayList<>();

        for(EduSubject subject:allList){
            if(subject.getParentId().equals("0")){
                subject=find(subject,allList);
                list.add(subject);
            }
        }
        return ResultVo.success("success",list);
    }

    public EduSubject find(EduSubject subject,List<EduSubject> list){
        for(EduSubject sub:list) {
            if (subject.getId().equals(sub.getParentId())) {
                subject.getChildren().add(sub);
                find(sub, list);
            }
        }
        return subject;
    }

}
